package com.matrix.utils.common

import android.app.Activity
import android.app.ActivityManager
import android.app.ActivityManager.RunningTaskInfo
import android.content.Context
import android.os.Process
import java.util.*
import kotlin.system.exitProcess

/**
 * 作者：Administrator
 * 日期：2024/9/29 09:25
 * 描述：
 */
object ActivityManager {

    private var mActivityStack = Stack<Activity>()

    /**
     * 添加Activity到堆栈
     */
    fun addActivity(activity: Activity?) {
        if (null != mActivityStack) {
            mActivityStack.add(activity)
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    fun currentActivity(): Activity? {
        return if (null == mActivityStack) null else mActivityStack.lastElement()
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    fun finishActivity() {
        if (null != mActivityStack) {
            val activity = mActivityStack.lastElement()
            this.finishActivity(activity)
        }
    }

    /**
     * 结束指定的Activity
     */
     fun finishActivity(activity: Activity) {
        if (null != mActivityStack) {
            if (mActivityStack.contains(activity)) {
                mActivityStack.remove(activity)
                if (!activity.isFinishing) {
                    activity.finish()
                }
            }
        }
    }

    /**
     * 获取指定类名的Activity
     */
    fun findActivity(vararg activityClass: Class<out Activity?>?): Activity? {
        if (null != mActivityStack && !mActivityStack.isEmpty()) {
            for (activity in mActivityStack) {
                if (Arrays.asList(*activityClass).contains(activity.javaClass)) {
                    return activity
                }
            }
        }
        return null
    }

    /**
     * 结束指定类名的Activity
     */
    fun finishActivity(vararg activityClass: Class<out Activity?>?) {
        if (null != mActivityStack && !mActivityStack.isEmpty()) {
            val iterator = mActivityStack.iterator()
            while (iterator.hasNext()) {
                val activity = iterator.next()
                if (Arrays.asList(*activityClass).contains(activity.javaClass)) {
                    iterator.remove()
                    activity.finish()
                }
            }
        }
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        if (null != mActivityStack && mActivityStack.size > 0) {
            for (activity in mActivityStack) {
                activity.finish()
            }
            mActivityStack.clear()
        }
    }

    /**
     * 退出应用程序
     */
    fun AppExit() {
        finishAllActivity()
        mActivityStack.clear()
        Process.killProcess(Process.myPid())
        exitProcess(0)
    }

    /**
     * 是否退出应用程序
     *
     * @return
     */
    fun isAppExit(): Boolean {
        return null == mActivityStack || mActivityStack.isEmpty()
    }

    /**
     * 判断某个activity是否在栈顶
     *
     * @param context
     * @param activityClass 某个activity
     * @return
     */
    fun isTopActivity(context: Context?, activityClass: Class<*>?): Boolean {
        if (null == context || null == activityClass) {
            return false
        }
        val activityName = activityClass.name
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        var runningTaskInfoList: List<RunningTaskInfo>? = null
        if (am != null) {
            runningTaskInfoList = am.getRunningTasks(1)
        }
        if (runningTaskInfoList != null && runningTaskInfoList.size > 0) {
            val cpn = runningTaskInfoList[0].topActivity
            return activityName == cpn!!.className
        }
        return false
    }

    fun getActivitySizes(): Int {
        return mActivityStack.size
    }
}